$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/recvedioinfo/list',
        datatype: "json",
        colModel: [
            // { label: 'id', name: 'id', index: 'id', width: 50, key: true },

            {label: '标题', name: 'vTitle', index: 'v_title', width: 80},

            {
                label: '视频类型', name: 'vType', index: 'v_type', width: 80
                , formatter: function (cellvalue, options, rowObject) {
                    // debugger
                    if (rowObject.vType == 1) {
                        return "<h3>前端</h3>"
                    } else if (rowObject.vType == 2) {
                        return "<h3>后端</h3>"
                    } else {
                        return "<h3>未知类型</h3>"
                    }

                }
            },

            {label: '描述', name: 'vDesc', index: 'v_desc', width: 80},

            {
                label: '播放地址',
                name: 'vUrl',
                index: 'v_url',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {

                    return "<div class='vedioContent'>" + rowObject.vUrl + "</div>";

                }
            },


            // { label: '创建者id', name: 'ctreaeid', index: 'ctreaeid', width: 80 },


            // { label: '删除标记  -1：已删除  0：正常', name: 'delFlag', index: 'del_flag', width: 80 },


            {label: '创建时间', name: 'createtime', index: 'createtime', width: 80},


            {name: '操作', width: 50, edittype:"button", formatter: editRow} //操作列




        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function editRow(grid, rows, rowObject) {
    return "<button class='btn btn-primary ' onclick= \"edit(" + rowObject.id+" )\">编辑</button>" +

    "<button class='btn btn-primary ' onclick=\"deleted(" + rowObject.id+" )\">删除</button>"  ;

    ;
}

function edit( id){
    // alert(rowId)
    // var rowData = $("#jqGrid").jqGrid('getRowData',rowId);

    // var id = getSelectedRow();
    if (id == null) {
        return;
    }
    vm.showList = false;
    vm.title = "修改";

    vm.getInfo(id)
}


function deleted(id){
    // var rowData = $("#jqGrid").jqGrid('getRowData',rowId);
    // var ids = getSelectedRows();
    if (id == null) {
        return;
    }

    let ids = [] ;
    ids.push(id) ;

    var lock = false;
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        if (!lock) {
            lock = true;
            $.ajax({
                type: "POST",
                url: baseURL + "sys/recvedioinfo/delete",
                contentType: "application/json",
                data: JSON.stringify(ids),
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg("操作成功", {icon: 1});
                        $("#jqGrid").trigger("reloadGrid");
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
        }
    }, function () {
    });

}





var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        recVedioInfo: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.recVedioInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.recVedioInfo.id == null ? "sys/recvedioinfo/save" : "sys/recvedioinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.recVedioInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/recvedioinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/recvedioinfo/info/" + id, function (r) {
                vm.recVedioInfo = r.recVedioInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
